package com.yanqu.road.web.mgr;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.entity.servercenter.ChannelNotice;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.MyURLEncoderHelper;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class ChannelNoticeMgr extends TempMgr {

	private static Map<Integer, Map<String, ChannelNotice>> channelNoticeMap;

	private static Map<String, Map<String, ChannelNotice>> appIdChannelNoticeMap;

	@Override
	public boolean reloadData() {
		channelNoticeMap = ServerBussiness.getChannelNoticeMap();
		appIdChannelNoticeMap = ServerBussiness.getAppIdChannelNoticeMap();
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	@Override
	public boolean stop() {
		return false;
	}

	public static ChannelNotice getChannelNotice(String appId, String language) {
		if (appIdChannelNoticeMap.get(appId) == null) {
			return null;
		}
		return appIdChannelNoticeMap.get(appId).get(language);
	}

	public static ChannelNotice getChannelNotice(int channelId, String language) {
		if (channelNoticeMap.get(channelId) == null) {
			return null;
		}
		return channelNoticeMap.get(channelId).get(language);
	}

	public static JSONObject parseNoticeJson(ChannelNotice channelNotice){
		JSONObject dataObj = new JSONObject();
		if (channelNotice != null) {
			dataObj.put("id", channelNotice.getId());
			try {
				dataObj.put("content", MyURLEncoderHelper.encode(channelNotice.getContent(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				dataObj.put("content", channelNotice.getContent());
			}
			try {
				dataObj.put("title", MyURLEncoderHelper.encode(channelNotice.getTitle(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				dataObj.put("title", channelNotice.getTitle());
			}
		}
		return dataObj;
	}
}
